<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;
class Logger extends Controller
{
    public function login()
    {
    	$request = Request::instance();
    	$uid='';
    	$current_login_ip=$request->ip();//登录IP
    	$last_login_ip='';//上次登录ip
    	$current_login_time=date('Y-m-d H:i:s',time());////登录时间
    	if (Request::instance()->has('username','post') && Request::instance()->has('password','post')) {
			$username=Request::instance()->post('username');
			$password=md5(Request::instance()->post('password'));
			$admin_user=Db::table('admin_user')->where('username',$username)->where('password',$password)->where('status',1)->find();
			if ($admin_user) {
				$uid=$admin_user['id'];
				$last_login_ip=$admin_user['current_login_ip'];
				//修改上次登录时间以及IP
				$data = [
					'current_login_ip' => $current_login_ip,//登录IP
					'last_login_ip' =>$last_login_ip,//上次登录ip
					'current_login_time' => $current_login_time//登录时间
				];
        		Db::table('admin_user')->where('id',$uid)->update($data);
        		Session::set('user.uid',$admin_user['id']);
        		Session::set('user.username',$admin_user['username']);
        		Session::set('user.telphone',$admin_user['telphone']);
        		Session::set('user.email',$admin_user['email']);
        		Session::set('user.real_name',$admin_user['real_name']);

        		//获取权限组
        		$role=Db::table('auth_group_access')->field('group.id,group.title')
				->alias('access')
				->join('auth_group group','access.group_id = group.id')
				->where('access.uid',Session::get('user.uid'))
				->find();
				$group_id=$role['id'];
				$role=$role['title'];
				Session::set('jurisdiction.group_id',$group_id);
				Session::set('jurisdiction.role',$role);
				$arr['access_token']=$admin_user['access_token'];
				$this->result($arr,'0','登录成功');
			}else{
				$this->result('','102','账号密码错误');
			}
    	}else{
    		$this->result('','1001','请登录');
    	}
    }
    public function logout()
    {
    	Session::clear();
    	$this->result(null,0,'退出成功');
    }
     
}
